package config

import (
	"GoServer/utils/debug"
	"fmt"
	"github.com/spf13/viper"
)

var configs = make(map[string]*viper.Viper)

func InitConfigs() {
	LoadConfig("app", "config/app.yaml")
}
func LoadConfig(name, path string) {
	v := viper.New()
	v.SetConfigFile(path)

	if err := v.ReadInConfig(); err != nil {
		debug.Logln(debug.Error, fmt.Sprintf("❌ Failed to read config [%s]: %v", path, err))
		return
	}

	configs[name] = v
	debug.Logln(debug.Debug, fmt.Sprintf("Loaded config: %s", path))
}

func GetConfig(name string) *viper.Viper {
	if cfg, ok := configs[name]; ok {
		return cfg
	}

	return viper.New()
}

func AllowedIPs(clientIP string) bool {
	allowedIPs := viper.GetStringSlice("letsbusiness.ip")

	for _, ip := range allowedIPs {
		if clientIP == ip {
			return true
		}
	}

	return false
}
